package css.sword.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.autoconfigure.SpringBootVFS;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.StringUtils;


import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.IOException;

/**
 * MybatisPlus配置
 * Created by wangfan on 2018-02-22 上午 11:29.
 */
@EnableTransactionManagement
@Configuration
@MapperScan(basePackages = {"css.sword.mapper"})
@EnableConfigurationProperties(MybatisProperties.class)
public class MybatisPlusConfig {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Autowired(required = false)
    private Interceptor[] interceptors;

    @Autowired(required = false)
    private DatabaseIdProvider databaseIdProvider;

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        page.setDialectType("oracle");
        return page;

    }

    /**
     * 这里全部使用mybatis-autoconfigure 已经自动加载的资源。不手动指定
     * 配置文件和mybatis-boot的配置文件同步
     */
//    @Bean
//    public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() throws IOException {
//        MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
//        mybatisPlus.setDataSource(dataSource);
//        mybatisPlus.setVfs(SpringBootVFS.class);
//        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        mybatisPlus.setMapperLocations(resolver.getResources("classpath*:mapper/*Mapper.xml"));
//
//        if (!ObjectUtils.isEmpty(this.interceptors)) {
//            mybatisPlus.setPlugins(this.interceptors);
//        }
//        // MP 全局配置，更多内容进入类看注释
//        GlobalConfig globalConfig = new GlobalConfig();
//        globalConfig.setSqlSessionFactory(sqlSessionFactory);
//        GlobalConfig.DbConfig dbConfig= new GlobalConfig.DbConfig();
//        dbConfig.setDbType(DbType.ORACLE);
//
//        // ID 策略 AUTO->`0`("数据库ID自增") INPUT->`1`(用户输入ID") ID_WORKER->`2`("全局唯一ID") UUID->`3`("全局唯一ID")
//        //使用ID_WORKER_STR，因为前后端分离使用整形，前端JS会有精度丢失
//
//        dbConfig.setIdType(IdType.NONE);
//        globalConfig.setSqlInjector(sqlInjector());
//
//        globalConfig.setDbConfig(dbConfig);
//        dbConfig.setTablePrefix("OA_ZH.");
//        //MP 属性下划线 转 驼峰 , 如果原生配置 mc.setMapUnderscoreToCamelCase(true) 开启，该配置可以无。
//        //globalConfig.setDbColumnUnderline(true);
//        mybatisPlus.setGlobalConfig(globalConfig);
//        mybatisPlus.setPlugins(new Interceptor[]{performanceInterceptor(),paginationInterceptor()});
//        MybatisConfiguration mc = new MybatisConfiguration();
//        // 对于完全自定义的mapper需要加此项配置，才能实现下划线转驼峰
//        mc.setMapUnderscoreToCamelCase(true);
//        mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
//        mybatisPlus.setConfiguration(mc);
//        if (this.databaseIdProvider != null) {
//            mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
//        }
//
//        return mybatisPlus;
//    }


    /**
     * 注入sql注入器
     */
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }

//    //性能插件
//    @Bean
//    @Profile({"dev","prod"})
//    public PerformanceInterceptor performanceInterceptor(){
//        PerformanceInterceptor performanceInterceptor= new PerformanceInterceptor();
//        performanceInterceptor.setFormat(true);
//        performanceInterceptor.setMaxTime(20000);
//        return performanceInterceptor;
//    }

}